<?php
/*
Plugin Name: ev-widgets
Plugin URI: http://wp-jacob.googlecode.com
Description: Re-implements many of the standard widgets to provide for more features
Version: 0.2
Author: Jacob Brunson
Author URI: http://jacobbrunson.com
*/

/*  Copyright 2008  Jacob Brunson  (email : wp-jacob.gpl.license@jacobbrunson.com )

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/


function ev_widget_year_archives($args) {
	extract($args);
	$options = get_option('ev_widget_year_archives');
	$c = $options['count'] ? '1' : '0';
	$d = $options['dropdown'] ? '1' : '0';
	$title = empty($options['title']) ? __('Archives') : apply_filters('widget_title', $options['title']);

	echo $before_widget;
	echo $before_title . $title . $after_title;

	if($d) {
?>
		<select name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> <option value=""><?php echo attribute_escape(__('Select Month')); ?></option> <?php wp_get_archives("type=monthly&format=option&show_post_count=$c"); ?> </select>
<?php
	} else {
?>
		<ul>
		<?php wp_get_archives("type=yearly&show_post_count=$c"); ?>
		</ul>
<?php
	}

	echo $after_widget;
}

function ev_widget_year_archives_control() {
	$options = $newoptions = get_option('ev_widget_year_archives');
	if ( $_POST["year_archives-submit"] ) {
		$newoptions['count'] = isset($_POST['year_archives-count']);
		$newoptions['dropdown'] = isset($_POST['year_archives-dropdown']);
		$newoptions['title'] = strip_tags(stripslashes($_POST["year_archives-title"]));
	}
	if ( $options != $newoptions ) {
		$options = $newoptions;
		update_option('ev_widget_year_archives', $options);
	}
	$count = $options['count'] ? 'checked="checked"' : '';
	$dropdown = $options['dropdown'] ? 'checked="checked"' : '';
	$title = attribute_escape($options['title']);
?>
			<p><label for="year_archives-title"><?php _e('Title:'); ?> <input class="widefat" id="year_archives-title" name="year_archives-title" type="text" value="<?php echo $title; ?>" /></label></p>
			<p>
				<label for="year_archives-count"><input class="checkbox" type="checkbox" <?php echo $count; ?> id="year_archives-count" name="year_archives-count" /> <?php _e('Show post counts'); ?></label>
				<br />
				<label for="year_archives-dropdown"><input class="checkbox" type="checkbox" <?php echo $dropdown; ?> id="year_archives-dropdown" name="year_archives-dropdown" /> <?php _e('Display as a drop down'); ?></label>
			</p>
			<input type="hidden" id="year_archives-submit" name="year_archives-submit" value="1" />
<?php
}

function init_ev_widgets_plugin() {
	register_sidebar_widget('year_archives', 'ev_widget_year_archives');
	register_widget_control('year_archives', 'ev_widget_year_archives_control');
}
add_action('init', 'init_ev_widgets_plugin');